// const proxy = {
//   "/apiProxy": { 
//     "target": 'https://www.zhangcs.cn/api/', 
//     // "target": 'http://127.0.0.1:70/api/', 
//     "changeOrigin": true, 
//     "pathRewrite": {   
//       '^/apiProxy': ''
//     }
//   },
// }
// exports.proxy = proxy;

/**
 * 接口代理
 * @author zhangcs
 */

// 获取接口地址
function getTarget(type) {
  const obj = {   
    'dev'  : 'http://dev.zhangcs.cn',   
    'test' : 'https://test.zhangcs.cn',
    'prod' : 'https://zhangcs.cc', // nginx: 
    'springBoot_dev': 'http://127.0.0.1',
    'springBoot_prod': 'https://zhangcs.cc',
  };
  return obj[type];
};

// 微服务 - Spring Boot / Spring Cloud
const SERVICE = [
  {
    name: '/fileService',
    hostname: getTarget('springBoot_dev'),
    serviceName: '/fileService',
    port: 8083,
  },

  // 下面两个都是正式，效果是相同的
  {
    name: '/springBoot',
    hostname: getTarget('springBoot_prod'),
    serviceName: '/springBoot',
    port: null,
  },
  {
    name: '/springBoot/fileService',
    hostname: getTarget('springBoot_prod'),
    serviceName: '/springBoot/fileService',
    port: null,
  },
];

// 初始化 代理
function initProxy() {
  let proxy = {};
  SERVICE.forEach(item => {
    proxy[item.name] = {
      target: item.port ? `${item.hostname}:${item.port}${item.serviceName}` : `${item.hostname}${item.serviceName}`,
      changeOrigin: item.changeOrigin || true,
      pathRewrite: { 
        ['^' + item.name]: ''
      }
    }
  })
  return proxy;
}


module.exports = initProxy();